// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Y [author] rhyme_lph
// n [email]  rhymelph@gmail.com
// t [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/entities/leave_item_entity.dart';
import 'package:finance_app/src/provider/hr_payroll/payroll/leave_details_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/down_button_form.dart';
import 'package:finance_app/src/widgets/list_item/leaver_item.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "leaveDetailsPage")
class LeaveDetailsPage extends StatefulWidget {
  final List<EmployeeContactEntity> data;
  final String startDate;
  final String endDate;

  const LeaveDetailsPage({Key key, this.data, this.startDate, this.endDate})
      : super(key: key);
  @override
  _LeaveDetailsPageState createState() => _LeaveDetailsPageState();
}

class _LeaveDetailsPageState extends State<LeaveDetailsPage>
    with PagePresenter<LeaveDetailsPage, LeaveDetailsProvider> {
  @override
  void initState() {
    provider.employees = widget.data;
    provider.startDate = widget.startDate;
    provider.endDate = widget.endDate;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.leave_details,
      );

  @override
  Widget get buildEmptyWidget {
    return ColoredBox(
      color: Colors.white,
      child: Column(
        children: [
          AppSpaces.h20,
          listBuilder<String>(
            selector: (_, __) => __.currentSelectEmployee?.name,
            builder: (BuildContext context, String value, Widget child) =>
                DownButtonForm(
              title: value ?? S.current.all,
              onTap: provider.onChangeEmployee,
            ),
          ),
          AppSpaces.h20,
          AppSpaces.grayH05,
          Expanded(child: super.buildEmptyWidget)
        ],
      ),
    );
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            listBuilder<String>(
              selector: (_, __) => __.currentSelectEmployee?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  DownButtonForm(
                title: value ?? S.current.all,
                onTap: provider.onChangeEmployee,
              ),
            ),
            AppSpaces.h20,
            AppSpaces.grayH05,
            Expanded(
              child: listBuilder<List<LeaveItemEntity>>(
                  selector: (_, __) => __.data,
                  builder: (BuildContext context, List<LeaveItemEntity> data,
                          Widget child) =>
                      ListView.builder(
                          itemCount: provider.data.length,
                          itemBuilder: (
                            context,
                            i,
                          ) =>
                              buildItemWidget(provider.data[i]))),
            ),
          ],
        ),
      );

  Widget buildItemWidget(LeaveItemEntity e) {
    return PressedOpacityButton(
      onPressed: () => provider.onNavDetail(e),
      child: LeaverItem(
        name: e.employeeName,
        title: e.leaveTypeName,
        timeRange:
            '${DateTimeUtils.formatDateTimeString(e.startDate)} ${e.startDateMeridian} - ${DateTimeUtils.formatDateTimeString(e.endDate)} ${e.endDateMeridian}',
        status: provider.getLeaveStatus(e),
        day: e.days,
      ),
    );
  }
}
